<template>
	<xn-form-container
		:title="'分配核实人'"
		:width="1200"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-divider>上报信息</a-divider>
			<a-row :gutter="16">
				<a-col :span="6">
					<a-form-item label="上报人姓名：" name="userName">
						<a-input v-model:value="formData.userName" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="上报人电话：" name="userPhone">
						<a-input v-model:value="formData.userPhone" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="隐患类型" name="hiddenType">
						<a-select v-model:value="formData.hiddenType" :options="hiddenTypeOptions" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="隐患上报位置：" name="address">
						<a-input v-model:value="formData.address" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="隐患描述：" name="hiddenRemark">
						<a-textarea v-model:value="formData.hiddenRemark" :rows="4" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="图片附件" name="fliePath">
						<SingPicUpload ref="fliePathUploadRef" :limitImage="0"></SingPicUpload>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="视频文件：" name="videoPath">
						<UploadFile ref="videoPathUploadRef" :limitImage="0"></UploadFile>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="声音文件：" name="soundPath">
						<UploadFile ref="soundPathUploadRef" :limitImage="0"></UploadFile>
					</a-form-item>
				</a-col>
			</a-row>
			<a-divider>分配核实人</a-divider>
			<a-row :gutter="16">
				<a-col :span="6">
					<a-form-item label="核实人" name="verifyUserId">
						<a-select v-model:value="formData.verifyUserId" placeholder="请选择核实人"
								  :options="verifyUserOptions"
								  :field-names="{
										label: 'name',
										value: 'id'
									}"
								  allow-clear/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">分配核实人</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="hiddenReportForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import hiddenReportApi from '@/api/biz/hidden/hiddenReportApi'
	import tool from "@/utils/tool"
	import SingPicUpload from '@/components/SingPicUpload/index.vue'
	import UploadFile from '@/components/FileUpload/index.vue'
	import bizUserApi from "@/api/biz/bizUserApi";

	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const verifyUserOptions = ref([])
	const fliePathUploadRef = ref()
	const videoPathUploadRef = ref()
	const soundPathUploadRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		bizUserApi.listRoleUser( {'roleCode':'role_verify'}).then((res) => {
			verifyUserOptions.value = res
		})

		nextTick(() => {
			if (record && record.fliePath) {
				fliePathUploadRef.value.initFileList(record.fliePath)
			}
			if (record && record.videoPath) {
				videoPathUploadRef.value.initFileList(record.videoPath)
			}
			if (record && record.soundPath) {
				soundPathUploadRef.value.initFileList(record.soundPath)
			}
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		verifyUserId: [required('请选择核实人')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			hiddenReportApi.allocateVerifyUser(formDataParam)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
